package com.stn.zjx.givingtreasure.fragment.search;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hhl.library.FlowTagLayout;
import com.hhl.library.OnTagSelectListener;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.DropItemListAdapter;
import com.stn.zjx.givingtreasure.adapter.RightSelectTagAdapter;
import com.stn.zjx.givingtreasure.adapter.StoreItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseSearchResultFragment;
import com.stn.zjx.givingtreasure.demo.DropItem;
import com.stn.zjx.givingtreasure.demo.StoreShow;
import com.stn.zjx.givingtreasure.event.DropSelectEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class StoreSearchFragment extends BaseSearchResultFragment {

    public View rootView;


    public StoreItemListAdapter adapter;
    public LinearLayoutManager lm;


    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.drop_rv)
    RecyclerView dropRv;
    @BindView(R.id.under_select)
    LinearLayout underSelect;
    @BindView(R.id.multiple_order_tv)
    TextView multipleOrderTv;
    @BindView(R.id.multiple_order_iv)
    ImageView multipleOrderIv;
    @BindView(R.id.multiple_order_bt)
    RelativeLayout multipleOrderBt;
    @BindView(R.id.volume_order_tv)
    TextView volumeOrderTv;
    @BindView(R.id.volume_order_bt)
    RelativeLayout volumeOrderBt;
    @BindView(R.id.comment_number_tv)
    TextView commentNumberTv;
    @BindView(R.id.comment_number_bt)
    RelativeLayout commentNumberBt;
    @BindView(R.id.screen_bt)
    RelativeLayout screenBt;
    @BindView(R.id.under_select_layer)
    RelativeLayout underSelectLayer;
    @BindView(R.id.screen_right_head)
    View screenRightHead;
    @BindView(R.id.right_screen)
    LinearLayout rightScreen;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.sevice_flow_layout)
    FlowTagLayout seviceFlowLayout;
    @BindView(R.id.goods_pay_flow_layout)
    FlowTagLayout goodsPayFlowLayout;
    @BindView(R.id.right_screen_reset_bt)
    TextView rightScreenResetBt;
    @BindView(R.id.right_screen_sure_bt)
    TextView rightScreenSureBt;
    @BindView(R.id.store_type_flow_layout)
    FlowTagLayout storeTypeFlowLayout;


    private RightSelectTagAdapter serviceTagAdapter;
    private RightSelectTagAdapter goodsPayTagAdapter;

    private RightSelectTagAdapter storeTypeTagAdapter;
    public DropItemListAdapter drop_adapter;
    private LinearLayoutManager drop_lm;

    public int multi_pos = 0;
    public int drop_select_pos = 0;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDropSelect(DropSelectEvent event) {
        switch (event.flag) {
            case DropSelectEvent.Drop_Search_Result_Store_Select:
                multi_pos = event.position;
                DropItem dropItem = new Gson().fromJson(event.data, DropItem.class);
                multipleOrderTv.setText(dropItem.name);
                underSelect.setVisibility(View.GONE);
                multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                break;
        }
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        if (rootView == null) {
            rootView = View.inflate(activity, R.layout.fragment_search_store, null);
            ButterKnife.bind(this, rootView);
            initView();
        }
        return rootView;
    }

    public void initView() {
        lm = new LinearLayoutManager(activity);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(lm);
        rv.setAdapter(adapter = new StoreItemListAdapter(activity));

        drop_lm = new LinearLayoutManager(activity);
        drop_lm.setOrientation(LinearLayoutManager.VERTICAL);

        dropRv.setLayoutManager(drop_lm);
        dropRv.setAdapter(drop_adapter = new DropItemListAdapter(activity, DropSelectEvent.Drop_Search_Result_Store_Select));
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        serviceTagAdapter = new RightSelectTagAdapter(activity);
        seviceFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        seviceFlowLayout.setAdapter(serviceTagAdapter);
        seviceFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() == 0) {
                    return;
                }

            }
        });
        List<String> da = new ArrayList<>();
        da.add("折扣送");
        da.add("到店送");
        da.add("限量送");
        da.add("体验送");
        da.add("买就送");
        da.add("分享送");
        serviceTagAdapter.onlyAddAll(da);


        storeTypeTagAdapter = new RightSelectTagAdapter(activity);
        storeTypeFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        storeTypeFlowLayout.setAdapter(storeTypeTagAdapter);
        storeTypeFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() == 0) {

                    return;
                }
            }
        });
        List<String> storeTypes = new ArrayList<>();
        storeTypes.add("男装");
        storeTypes.add("女装");
        storeTypes.add("男鞋");
        storeTypes.add("女鞋");
        storeTypes.add("内衣配饰");
        storeTypes.add("箱包手袋");
        storeTypes.add("美妆护肤");
        storeTypes.add("个护清洁");
        storeTypes.add("钟表珠宝");
        storeTypes.add("手机数码");
        storeTypes.add("电脑办公");
        storeTypes.add("家用电器");
        storeTypes.add("食品生鲜");
        storeTypes.add("酒水饮料");
        storeTypes.add("母婴童装");
        storeTypes.add("玩具乐器");
        storeTypes.add("医药保健");
        storeTypes.add("计生情趣");
        storeTypes.add("运动户外");
        storeTypes.add("汽车用品");
        storeTypes.add("家具厨具");
        storeTypes.add("家具家装");
        storeTypes.add("礼品鲜花");
        storeTypes.add("宠物生活");
        storeTypes.add("艺术邮币");
        storeTypes.add("农资园艺");
        storeTypes.add("工艺品");

        storeTypeTagAdapter.onlyAddAll(storeTypes);



        goodsPayTagAdapter = new RightSelectTagAdapter(activity);
        goodsPayFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        goodsPayFlowLayout.setAdapter(goodsPayTagAdapter);
        goodsPayFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() == 0) {

                    return;
                }
            }
        });
        List<String> da1 = new ArrayList<>();
        da1.add("到店自取");
        da1.add("先买到货");
        da1.add("货到付款");
        goodsPayTagAdapter.onlyAddAll(da1);




        seviceFlowLayout.clearAllOption();
        goodsPayFlowLayout.clearAllOption();
        storeTypeFlowLayout.clearAllOption();

    }

    @Override
    public void initData() {
        List<DropItem> dropItems = new ArrayList<>();
        DropItem dropItem1 = new DropItem();
        DropItem dropItem2 = new DropItem();
        DropItem dropItem3 = new DropItem();
        dropItem1.orderName = getString(R.string.multiple_order);
        dropItem1.name = getString(R.string.multiple);
        dropItem2.orderName = getString(R.string.price_hight_low_order);
        dropItem2.name = getString(R.string.price_hight_low);
        dropItem3.orderName = getString(R.string.price_low_hight_order);
        dropItem3.name = getString(R.string.price_low_hight);
        dropItems.add(dropItem1);
        dropItems.add(dropItem2);
        dropItems.add(dropItem3);
        drop_adapter.pos = multi_pos;
        drop_adapter.refreshList(dropItems);
        underSelectLayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                underSelect.setVisibility(View.GONE);
            }
        });


         List<StoreShow> storeShows=new ArrayList<>();
         StoreShow sd1=new StoreShow();
          sd1.setGuestCount(15);
        sd1.setGuestRate("94%");
        sd1.setImg("https://img14.360buyimg.com/mobilecms/s280x280_jfs/t27547/124/2184135463/134006/ea43cc48/5bfbb603Nafea4dbd.jpg!q90");
        sd1.setIsBaoyou(1);
        sd1.setNowPrice("2980.00");
        sd1.setOriginPrice("3980.00");
        sd1.setSendType(3);

          List<String> tag1s=new ArrayList<>();
          tag1s.add("体闲时尚");
          tag1s.add("男士专用");
          sd1.setTag(tag1s);
          sd1.setTitle(" 三宅一生男士时尚休闲商务手表");
         StoreShow.StoreShowShop sss1=new StoreShow.StoreShowShop();
         sss1.setShopCity("深圳");
         sss1.setShopName("三宅一生手表自营店");
         sd1.setShop(sss1);
         storeShows.add(sd1);



        StoreShow sd2=new StoreShow();
        sd2.setGuestCount(4000);
        sd2.setGuestRate("99%");
        sd2.setImg("https://img11.360buyimg.com/mobilecms/s500x500_jfs/t29149/57/401007979/135325/68957ddb/5bf21ad8N4dc51226.jpg");
        sd2.setIsBaoyou(1);
        sd2.setNowPrice("29.80");
        sd2.setOriginPrice("88.00");
        sd2.setSendType(3);

        List<String> tag2s=new ArrayList<>();
        tag2s.add("绿茶");
        tag2s.add("口感好");
        sd2.setTag(tag2s);
        sd2.setTitle("2018新茶印象堂茶叶龙井茶 绿茶茶叶散装浓香豆香口碑茶陶瓷梅花龙头罐龙井茶");
        StoreShow.StoreShowShop sss2=new StoreShow.StoreShowShop();
        sss2.setShopCity("北京");
        sss2.setShopName("印象堂官方旗舰店");
        sd2.setShop(sss2);
        storeShows.add(sd2);

        StoreShow sd3=new StoreShow();
        sd3.setGuestCount(11000);
        sd3.setGuestRate("74%");
        sd3.setImg("https://img12.360buyimg.com/mobilecms/s500x500_jfs/t26887/305/2381132539/184869/30c8d6f/5bff5672Na525885f.jpg");
        sd3.setIsBaoyou(0);
        sd3.setNowPrice("0.00");
        sd3.setOriginPrice("48.90");
        sd3.setSendType(1);

        List<String> tag3s=new ArrayList<>();
        tag3s.add("味道好");
        tag3s.add("特产");
        sd3.setTag(tag3s);
        sd3.setTitle("高邮馆，到本店免费领取】南湖牌 高邮特产咸鸭蛋盐蛋实惠装 16枚 65g/枚 ");
        StoreShow.StoreShowShop sss3=new StoreShow.StoreShowShop();
        sss3.setShopCity("深圳");
        sss3.setShopName("中国特产·高邮馆");
        sd3.setShop(sss3);
        storeShows.add(sd3);


         adapter.refreshList(storeShows);
    }


    @OnClick({R.id.multiple_order_bt, R.id.volume_order_bt, R.id.comment_number_bt, R.id.screen_bt,
            R.id.right_screen_reset_bt, R.id.right_screen_sure_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.multiple_order_bt:
                switch (drop_select_pos) {
                    case 0:
                        if (underSelect.getVisibility() == View.VISIBLE) {
                            underSelect.setVisibility(View.GONE);
                            multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                            return;
                        }
                        multipleOrderIv.setImageResource(R.mipmap.order_red_up);
                        multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                        underSelect.setVisibility(View.VISIBLE);
                        break;
                    default:
                        commentNumberTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                        volumeOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));

                        multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                        multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                        drop_select_pos = 0;
                        break;
                }
                break;
            case R.id.volume_order_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }
                multipleOrderIv.setImageResource(R.mipmap.order_black_down);
                multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                commentNumberTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                volumeOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                drop_select_pos = 1;
                break;
            case R.id.comment_number_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }
                multipleOrderIv.setImageResource(R.mipmap.order_black_down);
                multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                volumeOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                commentNumberTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                drop_select_pos = 2;
                break;
            case R.id.screen_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
            case R.id.right_screen_reset_bt:
                seviceFlowLayout.clearAllOption();
                goodsPayFlowLayout.clearAllOption();
                break;
            case R.id.right_screen_sure_bt:
                drawerLayout.closeDrawer(Gravity.RIGHT);
                break;

        }
    }


}
